<?php
	include("includes/secure.php");
	include("includes/config.php");
	include("includes/class/adviser.class.php");
	
	$adviser = new Adviser(); 
	$ase_code	= $_GET['ase_code'];
	
	$msg_info = $msg_alert = $msg_error = false;
	$_SESSION['msg_cool'] = '';
	$show_msg = 0;
	
	$data = $adviser->getAdviserData($ase_code);
	$telephones = $adviser->getTelephones($ase_code);
	$countTelephones = count ($telephones);
	
	$states = $adviser->stateList();
	
	if(isset($_POST['edit']) && $_POST['edit'] == 1){
		$new_data->ase_code			= $_POST['ase_code'];
		$new_data->ase_name 		= trim(mysql_real_escape_string($_POST['ase_name']));
		$new_data->ase_ced 			= trim(mysql_real_escape_string($_POST['ase_ced']));
		$new_data->ase_email 		= trim(mysql_real_escape_string($_POST['ase_email']));
		
		if ( $adviser->updateAdviser($new_data) ){
			$_SESSION["msg_cool"] .= 'El Asesor fue actualizado..<br />';
			
			$data->id_fk = $_GET['ase_code'];
			
			if ( $adviser->deleteTelephones($data->id_fk) ){
				$countTel_number = count ( $_POST['tel_number'] );
				$i = 0;
				while ( $i < $countTel_number ){
					$data->tel_number = $_POST['tel_number'][$i];
					$data->tel_ext = $_POST['tel_ext'][$i];
					
					if ( ! $adviser->addTelephone($data) ){
						$msg_alert = true;
						$_SESSION["msg_cool"] .= 'Error adicionando telefonos para el asesor!!.<br />';
					}
						
					$i ++;
				}
				
				echo "<script> window.location.href='listAdviser.php?show_msg=3'</script>";
			}
			else{
				$_SESSION["msg_cool"] .= 'Error actualizando telefonos.<br />';
				echo "<script> window.location.href='listAdviser.php?show_msg=1'</script>";	
			}
		}
		else{
			$msg_error = true;
			$_SESSION["msg_cool"] .= 'No se pudo actualizar el Asesor...<br />';
		}		
	}
	
	if ( $msg_error )
		$show_msg = 1;
	elseif ( $msg_alert )
		$show_msg = 2;
	elseif ( $msg_info )
		$show_msg = 3;
?>